package com.lxl.model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

import com.lxl.main.Game;

/**
 * 敌军坦克类
 * 
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年11月3日 上午11:45:26
 */
public class EnemyTank {
	/**
	 * 坦克直径
	 */
	public static final int TANK_DIAMETER = 50;
	/**
	 * 坦克颜色
	 */
	public static final Color TANK_COLOR = Color.RED;
	/**
	 * 坦克移动速度
	 */
	public static final int TANK_SPEED = 2;
	/**
	 * 坦克炮筒颜色
	 */
	public static final Color TANK_BARREL_COLOR = Color.BLUE;
	/**
	 * 坦克炮筒长度
	 */
	public static final int TANK_BARREL_LENGTH = TANK_DIAMETER / 2;
	/**
	 * 坦克初始横坐标
	 */
	private int tankX = (int) (Math.random() * (Game.FRAME_WIDTH
			- TANK_DIAMETER - 200));
	/**
	 * 坦克初始纵坐标
	 */
	private int tankY = (int) (Math.random() * (Game.FRAME_HEIGHT - TANK_DIAMETER));
	/**
	 * 坦克最近一次的横坐标
	 */
	private int tankLastX = tankX;
	/**
	 * 坦克最近一次的纵坐标
	 */
	private int tankLastY = tankY;
	/**
	 * 坦克方向
	 */
	private Direction tankDirection = Direction.STOP;
	/**
	 * 坦克最近一次的方向
	 */
	private Direction tankLastDirection = Direction.UP;
	/**
	 * 定时改变坦克方向的线程
	 */
	private Thread changeDirectionThread = new Thread(new ChangeDirectionTask());
	/**
	 * 记录合法时间间隔内是否发射过炮弹的flag
	 */
	private boolean shootedFlag = false;
	/**
	 * 当前子弹
	 */
	private ArrayList<EnemyBullet> thisBullets = new ArrayList<EnemyBullet>();
	/**
	 * 坦克是否存活
	 */
	private boolean alive = true;

	/**
	 * 画坦克的方法
	 * 
	 * @param graphics
	 *            画笔
	 */
	public void paint(Graphics graphics) {
		// 如果死了就return
		if (!alive)
			return;
		if (!changeDirectionThread.isAlive())
			changeDirectionThread.start();
		// 移动坦克
		move();
		// 储存当前画笔颜色
		Color backGroundColor = graphics.getColor();
		// 改变当前画笔颜色为坦克颜色
		graphics.setColor(TANK_COLOR);
		// 画出坦克
		graphics.fillOval(tankX, tankY, TANK_DIAMETER, TANK_DIAMETER);
		// 画出炮筒
		paintBarrel(graphics);
		// 如果时间间隔内没发射过子弹，就打出一发子弹
		if (!shootedFlag) {
			shoot(graphics);
		}
		// 只要有子弹存在就画出所有子弹
		if (thisBullets.size() != 0)
			for (int i = 0; i < thisBullets.size(); i++)
				thisBullets.get(i).paint(graphics);
		// 还原当前画笔颜色
		graphics.setColor(backGroundColor);
	}

	/**
	 * 发射子弹方法
	 * 
	 * @param graphics
	 *            画笔
	 */
	private void shoot(Graphics graphics) {
		// 如果死了就return
		if (!alive)
			return;
		// 临时用方向
		Direction tempDirection;
		if (tankDirection == Direction.STOP)
			tempDirection = tankLastDirection;
		else
			tempDirection = tankDirection;
		// 坦克头横坐标
		int tankHeadX = tankX;
		// 坦克头纵坐标
		int tankHeadY = tankY;
		// 上
		if (tempDirection == Direction.UP) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
			tankHeadY = tankY - MyBullet.BULLET_DIAMETER;
		}
		// 下
		else if (tempDirection == Direction.DOWN) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
			tankHeadY = tankY + TANK_DIAMETER;
		}
		// 左
		else if (tempDirection == Direction.LEFT) {
			tankHeadX = tankX - MyBullet.BULLET_DIAMETER;
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
		}
		// 右
		else if (tempDirection == Direction.RIGHT) {
			tankHeadX = tankX + TANK_DIAMETER;
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 - MyBullet.BULLET_DIAMETER / 2.0);
		}
		// 上左
		else if (tempDirection == Direction.UP_LEFT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.1);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.1);
		}
		// 上右
		else if (tempDirection == Direction.UP_RIGHT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.1);
		}
		// 下左
		else if (tempDirection == Direction.DOWN_LEFT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.1);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
		}
		// 下右
		else if (tempDirection == Direction.DOWN_RIGHT) {
			tankHeadX = (int) (tankX + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
			tankHeadY = (int) (tankY + TANK_DIAMETER / 2.0 * 0.7 + TANK_DIAMETER / 2.0);
		}
		EnemyBullet thisBullet = new EnemyBullet(tempDirection, tankHeadX,
				tankHeadY);
		thisBullets.add(thisBullet);
		thisBullet.paint(graphics);
		thisBullet.setThisBullets(thisBullets);
		shootedFlag = true;
	}

	/**
	 * 撞墙处理方法
	 * 
	 * @param wall
	 *            欲判断的墙
	 */
	public void hitWall(Wall wall) {
		if (getSurroundRectangle().intersects(wall.getSurroundRectangle())) {
			// 回到过去
			backToLast();
		}
	}
	/**
	 * 回到上一位置方法
	 */
	public void backToLast() {
		tankX = tankLastX;
		tankY = tankLastY;
	}

	/**
	 * 坦克移动方法
	 */
	private void move() {
		// 如果死了就return
		if (!alive)
			return;
		// 将上一个X记录
		tankLastX = tankX;
		// 将上一个Y记录
		tankLastY = tankY;
		// 只有不越界的时候才能移动
		if ((tankX >= 0 && tankX <= Game.FRAME_WIDTH - TANK_DIAMETER)
				&& (tankY >= Game.HEADER_HEIGHT && tankY <= Game.FRAME_HEIGHT
						- TANK_DIAMETER)) {
			// 上
			if (tankDirection == Direction.UP) {
				tankY -= TANK_SPEED;
			}
			// 下
			else if (tankDirection == Direction.DOWN) {
				tankY += TANK_SPEED;
			}
			// 左
			else if (tankDirection == Direction.LEFT) {
				tankX -= TANK_SPEED;
			}
			// 右
			else if (tankDirection == Direction.RIGHT) {
				tankX += TANK_SPEED;
			}
			// 上左
			else if (tankDirection == Direction.UP_LEFT) {
				tankX -= TANK_SPEED * 0.7;
				tankY -= TANK_SPEED * 0.7;
			}
			// 上右
			else if (tankDirection == Direction.UP_RIGHT) {
				tankX += TANK_SPEED * 0.7;
				tankY -= TANK_SPEED * 0.7;
			}
			// 下左
			else if (tankDirection == Direction.DOWN_LEFT) {
				tankX -= TANK_SPEED * 0.7;
				tankY += TANK_SPEED * 0.7;
			}
			// 下右
			else if (tankDirection == Direction.DOWN_RIGHT) {
				tankX += TANK_SPEED * 0.7;
				tankY += TANK_SPEED * 0.7;
			}
		} else {
			if (tankX < 0)
				tankX = 0;
			if (tankX > Game.FRAME_WIDTH - TANK_DIAMETER)
				tankX = Game.FRAME_WIDTH - TANK_DIAMETER;
			if (tankY < Game.HEADER_HEIGHT)
				tankY = Game.HEADER_HEIGHT;
			if (tankY > Game.FRAME_HEIGHT - TANK_DIAMETER)
				tankY = Game.FRAME_HEIGHT - TANK_DIAMETER;
		}
	}

	/**
	 * 用于定时改变坦克方向的内部类
	 */
	private class ChangeDirectionTask implements Runnable {
		/**
		 * 重写run方法
		 */
		@Override
		public void run() {
			while (true) {
				// 改变坦克方向
				judgeTankDirection();
				try {
					// 每隔500毫秒改变一次
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 判断坦克方向方法
	 */
	private void judgeTankDirection() {
		// 如果死了就return
		if (!alive)
			return;
		// 如果是当前方向不是停止，将其记录为上一方向
		if (tankDirection != Direction.STOP)
			tankLastDirection = tankDirection;
		Direction[] directions = Direction.values();
		tankDirection = directions[(int) (Math.random() * directions.length)];

	}

	/**
	 * 画坦克炮筒方法
	 * 
	 * @param graphics
	 *            画笔
	 */
	private void paintBarrel(Graphics graphics) {
		// 如果死了就return
		if (!alive)
			return;
		// 坦克中心横坐标
		int tankCenterX = tankX + TANK_DIAMETER / 2;
		// 坦克中心纵坐标
		int tankCenterY = tankY + TANK_DIAMETER / 2;
		// 上
		if (tankDirection == Direction.UP) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
					tankCenterY - TANK_BARREL_LENGTH);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 下
		else if (tankDirection == Direction.DOWN) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
					tankCenterY + TANK_BARREL_LENGTH);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 左
		else if (tankDirection == Direction.LEFT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
					- TANK_BARREL_LENGTH, tankCenterY);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 右
		else if (tankDirection == Direction.RIGHT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
					+ TANK_BARREL_LENGTH, tankCenterY);
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 上左
		else if (tankDirection == Direction.UP_LEFT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 上右
		else if (tankDirection == Direction.UP_RIGHT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 下左
		else if (tankDirection == Direction.DOWN_LEFT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 下右
		else if (tankDirection == Direction.DOWN_RIGHT) {
			// 储存当前画笔颜色
			Color backGroundColor = graphics.getColor();
			// 改变当前画笔颜色为炮筒颜色
			graphics.setColor(TANK_BARREL_COLOR);
			// 画出炮筒
			graphics.drawLine(tankCenterX, tankCenterY,
					(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
					(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
			// 还原双缓冲图片当前画笔颜色
			graphics.setColor(backGroundColor);
		}
		// 停止
		else if (tankDirection == Direction.STOP) {
			if (tankLastDirection == Direction.UP) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
						tankCenterY - TANK_BARREL_LENGTH);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 下
			else if (tankLastDirection == Direction.DOWN) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX,
						tankCenterY + TANK_BARREL_LENGTH);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 左
			else if (tankLastDirection == Direction.LEFT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
						- TANK_BARREL_LENGTH, tankCenterY);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 右
			else if (tankLastDirection == Direction.RIGHT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY, tankCenterX
						+ TANK_BARREL_LENGTH, tankCenterY);
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 上左
			else if (tankLastDirection == Direction.UP_LEFT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 上右
			else if (tankLastDirection == Direction.UP_RIGHT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY - TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 下左
			else if (tankLastDirection == Direction.DOWN_LEFT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX - TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
			// 下右
			else if (tankLastDirection == Direction.DOWN_RIGHT) {
				// 储存当前画笔颜色
				Color backGroundColor = graphics.getColor();
				// 改变当前画笔颜色为炮筒颜色
				graphics.setColor(TANK_BARREL_COLOR);
				// 画出炮筒
				graphics.drawLine(tankCenterX, tankCenterY,
						(int) (tankCenterX + TANK_BARREL_LENGTH * 0.7),
						(int) (tankCenterY + TANK_BARREL_LENGTH * 0.7));
				// 还原双缓冲图片当前画笔颜色
				graphics.setColor(backGroundColor);
			}
		}
	}

	/**
	 * 判断子弹是否击中敌人
	 * 
	 * @param myTank
	 *            我军坦克
	 */
	public void hit(MyTank myTank) {
		// 如果死了就return
		if (!alive)
			return;

		// 如果是子弹撞到敌人
		for (int i = 0; i < thisBullets.size(); i++)
			// 调用当前弹夹的hit方法，判断是否击中
			thisBullets.get(i).hit(myTank);
	}

	/**
	 * 判断自己是否撞到自己人
	 * 
	 * @param enemyTanks
	 *            敌军坦克
	 */
	public void hitFriends(ArrayList<EnemyTank> enemyTanks) {
		// 如果是自己撞到自己人
		for (int i = 0; i < enemyTanks.size(); i++) {
			if (enemyTanks.get(i) == this)
				continue;
			if (getSurroundRectangle().intersects(
					enemyTanks.get(i).getSurroundRectangle())) {
				backToLast();
				enemyTanks.get(i).backToLast();
			}
		}
	}

	/**
	 * 获得围绕当前圆形的矩形
	 * 
	 * @return 围绕当前圆形的矩形
	 */
	public Rectangle getSurroundRectangle() {
		return new Rectangle(tankX, tankY, TANK_DIAMETER, TANK_DIAMETER);
	}

	/**
	 * shootedFlag的setter
	 * 
	 * @param shootedFlag
	 *            允许时间间隔内是否射击过
	 */
	public void setShootedFlag(boolean shootedFlag) {
		this.shootedFlag = shootedFlag;
	}

	/**
	 * alive的setter
	 * 
	 * @param alive
	 *            是否存活
	 */
	public void setAlive(boolean alive) {
		this.alive = alive;
	}

	/**
	 * alive的getter
	 * 
	 * @return true为存活
	 */
	public boolean isAlive() {
		return alive;
	}

	/**
	 * thisBullets的getter
	 * 
	 * @return 当前弹夹
	 */
	public ArrayList<EnemyBullet> getThisBullets() {
		return thisBullets;
	}
}
